#coding=utf-8
from stscraper import get_shstcode, get_szstcode
from stutils import set_django_environ
set_django_environ()

from stdj.stock.models import Stcode, Share
import datetime
td = datetime.date.today().isoformat()

for s in Stcode.objects.filter(created_at=td).order_by('code'):
    try:
        share = Share.objects.get(code = s.code)
    except Share.DoesNotExist,e:
        
        share = Share(code=s.code, name=s.name)
        share.save()
        
        print "New:%s(%s)"%(s.name, s.code)
        print 'Ipo day:%s'%s.ipo.isoformat()
        
        share = Share.objects.get(code = s.code)
        share.ts2db()
        share.histprice2db()
        
    else:
        if share.name != s.name:
            old = share.name
            share.name = s.name
            share.save()
            print "%s: (name change) %s --> %s"%(share.code, old, share.name)
